<template>
  <div>
    <van-nav-bar title="今日头条-登录" />
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="user.mobile"
          name="mobile"
          required
          label="手机号"
          placeholder="请输入11位手机号"
          :rules="[
            {
              required: true,
              message: '请填写手机号',
              pattern: /^1[3-9]\d{9}$/
            }
          ]"
        />
        <van-field
          v-model="user.code"
          type="password"
          name="code"
          required
          label="密码"
          placeholder="请输入6位密码"
          :rules="[
            { required: true, message: '请填写密码', pattern: /^\d{6}$/ }
          ]"
        />
        <div style="margin: 16px">
          <van-button
           round
           block
           :disabled='isLoading'
           :loading='isLoading'
           loading-text="登录加载中..."
           type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import Notify from '@/ui/Notify.js'
import { setToken } from '@/utils/token.js'
import { setStorage } from '@/utils/storage'

export default {
  data() {
    return {
      user: {
        mobile: '',
        code: ''
      },
      isLoading: false
    }
  },
  methods: {
    async onSubmit(values) {
      console.log('submit', values)
      // 禁止再次点击
      this.isLoading = true
      // 发起网络请求
      try {
        const res = await loginAPI(this.user)
        console.log(res)
        Notify({ type: 'success', message: '登录成功啦!!!' })
        setToken(res.data.data.token)
        setStorage('refresh_token', res.data.data.refresh_token)
        // 路由跳转到首页
        this.$router.replace({
          path: this.$route.query.path || '/layout/home'
        })
      } catch (err) {
        Notify({ type: 'danger', message: '账号或密码错误' })
      }
      this.isLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
// .van-nav-bar{
//   background-color: #007bff;
// }
// /deep/.van-nav-bar__title{
//   color: #fff
// }
</style>
